<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account extends Modules_Controller
{

	function __construct()
	{
		parent::__construct();

		$this->rules = array(
			"rule" => array(

			)
		);

	}
    /**
     * 列表页面
     * @param  int $cid  栏目ID
     * @param  integer $page 分页ID
     * @return html view 输出
     */
    public function index($cid=false,$page=1)
    {
        // 栏目路径
        $vdata['cpath']= $this->mcol->get_path_more($this->cid);
        $vdata['cchildren'] = $this->mcol->get_cols($this->cid);
        $title = $this->mcol->get_one($this->cid,"title");
        $vdata['title'] = $title['title'];

        $limit = $this->page_limit;
        $this->input->get('limit',TRUE) and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');

        $order = $this->_index_orders();
        if ($this->input->get('order',TRUE)) {
            // TODO: order
            // $orders = explode("-",$this->input->get('order',TRUE));
            $order = $this->input->get('order',TRUE);
        }
        $where_in = array("cid"=>$this->cid,"ccid"=>$this->ccid);
        // 条件必须
        $where = array_merge($this->_index_where(),$where_in);
        $vdata['pages'] = $this->_pages(site_url($this->class.'/index/'.$this->cid.'/'),$limit,$where,4);
        $vdata['list'] = $this->model->get_list($limit,$limit*($page-1),$order,$where);
        foreach($vdata['list'] as &$row) {
            if($row['company']) {
                $this->load->model('company_model','mcompany');
                $company = $this->mcompany->get_one($row['company']);
                $row['com_short'] = $company['short'];
            }
        }
        $total = $this->model->get_status_nums();
        $vdata['total'] = $total['total'];
        $this->_display($vdata);
    }
	public function copypro()
    {
        $ids = $this->input->post('ids');

        $rs=$this->model->get_one($ids);

        unset($rs['id']);
        unset($rs['sort_id']);
        unset($rs['timeline']);

        $id = $this->model->create($rs);
        if ($id) {
            $vdata['msg'] = '复制成功，请刷新查看';
            $vdata['status'] = 1;
        }else{
            $vdata['msg'] = '复制失败，请刷新后重试';
            $vdata['status'] = 0;
        }
        $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
    }
    public function status() {
        $key = '';
        if ($this->input->post('ids') ) {
            $key = explode(',',$this->input->post('ids'));
        }else{
            $vdata = array('status'=>0,'msg'=>lang('modules_no_id'));
        }

        $where = FALSE;

        if ($this->input->get('c')) {
            $where = array('cid' => $this->input->get('c'));
        }else{
            $vdata = array('status'=>0,'msg'=>lang('modules_no_col_id'));
        }

        $audit = $this->input->post('status');

        $msg = array(lang('modules_status_false'),lang('modules_status_loading'),lang('modules_status_true'));

        if (!isset($vdata['status'])) {
            foreach($key as $id) {
                $res = $this->model->set($id,array('status'=>$audit));
            }
            if ($res) {
                $vdata = array('status'=>1,'msg'=>$msg[$audit]);
                if (is_array($key)) {
                    $this->mlogs->add('status',lang('modules_status_id').$this->input->post('ids').lang('modules_status_for').$audit);
                }else{
                    $this->mlogs->add('status',lang('modules_status_id').$key.lang('modules_status_for').$audit);
                }
            }else{
                $vdata = array('status'=>0,'msg'=>lang('modules_status_err_select'));
            }
        }

        if($this->input->is_ajax_request()){
            $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
        }else{
            $this->load->view('msg',$vdata);
        }
    }
    public function audit($key = FALSE){
        if (!$key AND $this->input->post('ids') ) {
            $key = explode(',',$this->input->post('ids'));
        }else{
            $vdata = array('status'=>0,'msg'=>lang('modules_no_id'));
        }

        $where = FALSE;

        if ($this->input->get('c')) {
            $where = array('cid' => $this->input->get('c'));
        }else{
            $vdata = array('status'=>0,'msg'=>lang('modules_no_col_id'));
        }

        $audit = $this->input->post('audit');

        if ($audit) {
            $audit=1;
            $this->load->model('account_token_model','maccount_token');
            $this->maccount_token->gettoken_admin($key[0],'fresh');
        }else{
            $audit=0;
        }
        $msg = array(lang('modules_audit_false1'),lang('modules_audit_true1'));

        if (!isset($vdata['status'])) {
            if ($where) {
                $res = $this->model->audit($audit,$key,$where);
            }else{
                $res = $this->model->audit($audit,$key);
            }

            if ($res) {
                $vdata = array('status'=>1,'msg'=>$msg[$audit]);
                if (is_array($key)) {
                    $this->mlogs->add('audit',lang('modules_audit_id1').$this->input->post('ids').lang('modules_audit_for1').$audit);
                }else{
                    $this->mlogs->add('audit',lang('modules_audit_id1').$key.lang('modules_audit_for1').$audit);
                }
            }else{
                $vdata = array('status'=>0,'msg'=>lang('modules_audit_err_select1'));
            }
        }

        if($this->input->is_ajax_request()){
            $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
        }else{
            $this->load->view('msg',$vdata);
        }
    }
    protected function _index_where(){
        $arr =array();

        $where = array();
        $data = $this->input->get();
        if (isset($_GET['ctype']) && $_GET['ctype']) {
            $where['ctype'] = $_GET['ctype'];
        }
        if(isset($data['begin']) && $data['begin']) {
            $where['reg_time >='] = strtotime($data['begin']);
        }
        if(isset($data['end']) && $data['end']) {
            $where['reg_time <='] = strtotime($data['end']);
        }
        if(isset($data['uname']) && $data['uname']) {
            $where['like'] = array('uname', $data['uname']);
        }
        if(isset($data['phone']) && $data['phone']) {
            $where['phone'] = $data['phone'];
        }
        if(isset($data['com_name']) && $data['com_name']) {
            $this->load->model('company_model','mcompany');
            $coms = $this->mcompany->get_all(array('like' => array('name',$data['com_name'])),'id');
            $ids = array();
            foreach($coms as $row) {
                $ids[] = $row['id'];
            }
            if($ids) {
                $where['in'] = array('company', $ids);
            }
        }
        return $where;
        //return $arr;
    }
    // 搜索
    public function search($cid=false,$page=1){
        $vdata['cpath']= $this->mcol->get_path_more($this->cid);
        $vdata['cchildren'] = $this->mcol->get_cols($this->cid);
        $title = $this->mcol->get_one($this->cid,"title");
        $vdata['title'] = $title['title'];
        $limit = $this->page_limit;
        $this->input->get('limit',TRUE) and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');
        $order = $this->_index_orders();
        if ($this->input->get('order',TRUE)) {
            // TODO: order
            // $orders = explode("-",$this->input->get('order',TRUE));
            $order = $this->input->get('order',TRUE);
        }
        $where = array();
        $where['cid'] = $this->cid;
        if ($ctype = $this->input->get('ctype',TRUE) AND is_numeric($ctype)) {
            $ctypes_ids = $this->mctypes->find_ids($this->cid,$ctype);
            $where['in'] = array('ctype',$ctypes_ids);
        }
        if ($wh = $this->_search_where()) {
            $where = array_merge($where,$wh);
        }
        if($this->input->get('q',TRUE)){
            $where_q = array(
                'like title'=> array('title',$this->input->get('q',TRUE))
            );
            $where = array_merge($where,$where_q);
        }
        $vdata['pages'] = $this->_pages(site_url($this->class.'/search/'.$this->cid.'/'),$limit,$where,4);
        $vdata['list'] = $this->model->get_list($limit,$limit*($page-1),$order,$where);
        $this->_display($vdata,$this->class.'_index.php');
    }

    // 为search提供where条件数组
    protected function _search_where(){
        $where = array();
        $data = $this->input->get();
        if(isset($data['begin']) && $data['begin']) {
            $where['reg_time >='] = strtotime($data['begin']);
        }
        if(isset($data['end']) && $data['end']) {
            $where['reg_time <='] = strtotime($data['end']);
        }
        if(isset($data['uname']) && $data['uname']) {
            $where['like'] = array('uname', $data['uname']);
        }
        if(isset($data['phone']) && $data['phone']) {
            $where['phone'] = $data['phone'];
        }
        if(isset($data['com_name']) && $data['com_name']) {
            $this->load->model('company_model','mcompany');
            $coms = $this->mcompany->get_all(array('like' => array('name',$data['com_name'])),'id');
            $ids = array();
            foreach($coms as $row) {
                $ids[] = $row['id'];
            }
            if($ids) {
                $where['in'] = array('company', $ids);
            }
        }
        return $where;
    }
    protected function _index_orders(){
        return array( 'reg_time'=>'desc' );
    }
    protected function edit($key=false){
        if (!$key) {
            $key = $this->input->get_post('id',TRUE);
            if ($this->input->is_ajax_request()){
                if (!$key) {
                    $vdata = array('msg'=>'没有提供标识','status'=>0);
                    $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
                }
            }else{
                if (!$key) {
                    if (isset($this->cid)) {
                        $index = '/index/'.$this->cid;
                    }else{
                        $index = '/index';
                    }
                    redirect(site_url($this->class.$index));
                }
            }
        }
        $this->form_validation->set_rules($this->_get_rule('edit'));
        if ($this->form_validation->run() == false) {
            $vdata['it'] = $this->model->get_one($key);
            $this->load->model('account_info_model','maccount_info');
            $info = $this->maccount_info->get_one(array('uid'=>$key));
            $is_success = $this->is_success($key);
            $vdata['info'] = get_tags_title($vdata['it']['ctype'],$info,$is_success);
            $this->load->model('company_model','mcompany');
            $vdata['company'] = $this->mcompany->get_one($vdata['it']['company'],'name,short');
            if (!$vdata['it']) {
                $vdata = array('msg'=>'提供的标示是不存在的','status'=>0);
                if ($this->input->is_ajax_request()) {
                    $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
                }else{
                    $this->load->view('msg',$vdata);
                    return false;
                }
            }

            if ($this->input->is_ajax_request()) {
                if (is_post()) {
                    $vdata['status'] = 0;
                    $vdata['msg'] = validation_errors();
                }
                $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
            }else{
                $this->_display($vdata);
            }
        }else{
            $this->_edit();
        }
    }
    public function one() {
        $data = $this->input->get();
        if($data['status'] == 2) {
            $success = $this->is_success($data['id']);
            if(!$success) {
                goto_message('此用户还未完成注册第三步！！');
                return false;
            }
        }
        $res = $this->model->set($data['id'],array('status'=>$data['status']));
        if($res) {
            $this->load->model('smscode_model', 'msmscode');
            $account = $this->model->get_one($data['id'],'phone,ctype');
            if($data['status'] == 2) {
                $this->msmscode->sendCode($account['phone'], 6,6);
                //getui_push($data['id'],2,$data['id'],'尊敬的用户：您的账户已通过审核，账户可正常使用。');
            } else {
                $this->msmscode->sendCode($account['phone'], 3,3);
                $this->load->model('account_token_model','maccount_token');
                $this->maccount_token->gettoken_admin($data['id'],'fresh');
            }
            if($account['ctype'] == 1) {
                $this->_status_after($data['id'],$data['status']);
            }
            //$this->_status_after($data['id'],$data['status']);
            redirect(base64_decode($data['ref']));
        }
    }
    /**
     * 极光推送
     */
    public function _status_after($uid,$status) {
        //0审核不通过1正在审核2审核通过
        $account = $this->model->get_one($uid,'company');
        $this->load->model('company_model','mcompany');
        $company = $this->mcompany->get_one($account['company'],'uid');
        if($status == 2) {
            if(!$company['uid'] ) {
                $this->mcompany->update(array('uid'=>$uid),array('id'=>$account['company']));
            }
        } else {
            if($company['uid'] && $company['uid'] == $uid) {
                $this->mcompany->update(array('uid'=>0),array('id'=>$account['company']));
            }
        }
    }
    /**
     * 是否完成第三步
     */
    public function is_success($uid) {
        $info = $this->model->get_one($uid);
        $info_data = $this->maccountInfo->get_one(array('uid'=>$uid));
        $success = 0;
        if($info_data) {
            switch($info['ctype']) {
                case 1:
                    $success = $info_data['industry'] && $info_data['tags'] ? 1 : 0;
                    break;
                case 2:
                    $success = $info_data['interested'] && $info_data['fund_stage'] && $info_data['single_project'] ? 1 : 0;
                    break;
                case 3:
                    $success = $info_data['industry'] && $info_data['interested'] ? 1 : 0;
                    break;
                case 4:
                    $success = $info_data['good_industry'] && $info_data['interested'] ? 1 : 0;
                    break;
                case 5:
                    $success = $info_data['interested'] ? 1 : 0;
                    break;
                case 6:
                    //$info_data['business_type'] = get_mcategory_titles($info_data['business_type']);
                    $success = $info_data['interested'] ? 1 : 0;
                    break;
            }
        }
        return $success;
    }
}
